package tr.edu.yildiz.ce.dap.appagent.command.prompt;

import java.util.List;
import java.util.StringTokenizer;

import tr.edu.yildiz.ce.dap.appagent.command.deployment.agent.ListCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.AbstractAgentCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.CommandConstants;
import tr.edu.yildiz.ce.dap.appagent.exception.BasicAgentException;
import tr.edu.yildiz.ce.dap.appagent.model.App;
import tr.edu.yildiz.ce.dap.appagent.model.DapMessage;
import tr.edu.yildiz.ce.dap.appagent.process.base.AbstractProcess;

public class ListPromptCommand extends BasicPromptCommand{

	public ListPromptCommand(AbstractProcess process) {
		super(process);
	}

	@Override
	protected AbstractAgentCommand prepareAgentCommand(AbstractProcess process,String commandStr) throws BasicAgentException {
		StringTokenizer sTokenizer = new StringTokenizer(commandStr," ");
		String command = sTokenizer.nextToken();
		String name = null;
		String version = null;
		while (sTokenizer.hasMoreElements()) {
			String pType = (String) sTokenizer.nextElement();
			if (pType.equals(CommandConstants.PARAMETER_TYPE.NAME)){
				name = sTokenizer.nextToken();
			}else if (pType.equals(CommandConstants.PARAMETER_TYPE.VERSION)){
				version = sTokenizer.nextToken(); 
			}
		}
		return new ListCommand(process, name, version);
	}

	@Override
	protected void processResponse(DapMessage response) {
		List<App> appList = response.getAppList().getApp();
		for (App app : appList) {
			System.out.println(app.getName() + " " + app.getVersion() );
		}
	}

}
